<?php

/* 店铺等级 sgrade */

class UgradeModel extends BaseModel {

    var $table = 'ugrade';
    var $prikey = 'grade_id';
    var $_name = 'ugrade';

    /*
     * 判断名称是否唯一
     */

    function unique($field, $value, $grade_id = 0) {
        $conditions = " " . $field . " = '" . $value . "'";
        $grade_id && $conditions .= " AND grade_id <> '" . $grade_id . "'";
        return count($this->find(array('conditions' => $conditions))) == 0;
    }

    function get_option($fields = 'grade') {
        $ugrades = $this->find(array('order' => 'grade_id ASC'));
        $data = array();
        if ($ugrades) {
            foreach ($ugrades as $key => $val) {
                $data[$val['grade_id']] = $val[$fields];
            }
        }
        return $data;
    }

    function get_arange_growth($grade, $growth_needed) {
        if (!$grade) {
            return;
        }
        $last_grade = $this->get(array('conditions' => 'grade=' . $grade - 1));
        $data = $growth_needed + $last_grade['floor_growth'];
        return $data;
    }
    
    //更新对应会员所对应的会员等级
    function update_ugrade($user_id)
    {
        
        $member_mod = &m('member');
        $user = $member_mod->get($user_id);
        if(empty($user)){
            exit('error');
        }
        $ugrade = $this->get(array('conditions' => "growth_needed <= " . $user['total_integral'], 'field' => 'grade','order'=>'growth_needed desc'));
        $member_mod->edit($user['user_id'], array('ugrade' => $ugrade['grade'])); //更新等级
    }
    
}

?>